/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bd;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Pablo
 */
public class Consultas {

    private static Session session = null;
    private static Transaction tx = null;

    public static void abrirSessao() {
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            if (session.isOpen()) {
                System.out.println("O Hibernate está pronto para trabalhar.");
            }
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            System.out.println("ERRO ao inicializar a sessão do Hibernate");
            System.out.println(e.getMessage());
        } finally {
            //session.close();
        }
    }

    public static List getListObjetos(String sql) {
        try {
            if (!session.isOpen()) {
                session = HibernateUtil.getSessionFactory().openSession();
            }
            tx = session.beginTransaction();

            System.out.println("[CONSULTA - getListObjetos] " + sql);
            Query q = session.createQuery(sql);
            List resultList = q.list();
            //tx.commit();
            return resultList;
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            System.out.println("ERRO do Hibernate");
            System.out.println(e.getMessage());
            return null;
        } finally {
            //session.close();
        }
    }

    public static boolean salvaNoBanco(Object objeto) {
        try {
            if (!session.isOpen()) {
                session = HibernateUtil.getSessionFactory().openSession();
            }
            tx = session.beginTransaction();
            //session.update(objeto);
            //session.saveOrUpdate(objeto);
           // session.persist(objeto);
            session.merge(objeto);
            tx.commit();
            //session.flush();
            return true;
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            System.out.println("ERRO do Hibernate");
            System.out.println(e.getMessage());
            return false;
        } finally {
            // session.close();
        }
    }

    public static Object getUmaLinha(String sql) {
        try {
            if (!session.isOpen()) {
                session = HibernateUtil.getSessionFactory().openSession();
            }
            tx = session.beginTransaction();
            Query q = session.createQuery(sql);
            System.out.println("[CONSULTA - getUmaLinha] " + sql);
            // tx.commit();
            return q.uniqueResult();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            System.out.println("ERRO do Hibernate");
            System.out.println(e.getMessage());
            return null;
        } finally {
            //session.close();
        }
    }

    public static int getCount(String sql) {
        try {
            if (!session.isOpen()) {
                session = HibernateUtil.getSessionFactory().openSession();
            }
            tx = session.beginTransaction();
            System.out.println("[CONSULTA - getCount] " + sql);
            Query q = session.createQuery(sql);
            long count = (long) q.uniqueResult();
            int a = (int) count;
            // tx.commit();
            return a;
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            System.out.println("ERRO do Hibernate");
            System.out.println(e.getMessage());
            return 0;
        } finally {
            // session.close();
        }
    }

    public static boolean removeDoBanco(Object objeto) {
        try {
            if (!session.isOpen()) {
                session = HibernateUtil.getSessionFactory().openSession();
            }
            tx = session.beginTransaction();
            session.delete(objeto);
            tx.commit();
            return true;
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            System.out.println("ERRO do Hibernate");
            System.out.println(e.getMessage());
            return false;
        } finally {
            // session.close();
        }
    }
}
